/* The code examples copyrighted by Nokia Corporation that are included to
 * this material are licensed to you under following MIT-style License:
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import Qt 4.7
import com.meego 1.0

Page {
    id: page
    anchors.fill: parent
    property int fontSize: 18
    anchors.margins: 10
    tools:
            ToolBarLayout {
                    ToolItem {
                    iconId: "toolbar-view-menu"; onClicked: myMenu.open();
                    anchors.right: parent==undefined ? undefined : parent.right
                    }
            }

    function openFile(file) {
        var component = Qt.createComponent(file)

        if (component.status == Component.Ready)
            pageStack.push(component);
        else
            console.log("Error loading component:", component.errorString());
    }

    QueryDialog {
            id: errorDialog
            titleText: "Error"
            acceptButtonText: "Ok"
    }

    Connections {
        target: SignonExample
        onShowError: {
            errorDialog.message = SignonExample.errorMessage()
            errorDialog.open()
        }
    }

    Column {
        spacing: 10
        Row {
            id: methodsRow
            width: page.width
            spacing:  10
            Column {
                Text {text: "<b>Methods: </b>"; font.pointSize: fontSize}

                ListView {
                    id: methodsView
                    width: (page.width - methodsRow.spacing) / 2
                    height: page.height / 3
                    model: MethodsAvailable
                    clip: true
                    delegate: Text { text: modelData; font.pointSize: fontSize; width: parent.width
                        MouseArea { anchors.fill: parent;
                            onClicked: {
                                methodsView.currentIndex = index;
                                SignonExample.showAvailableMechanisms(parent.text)
                            }
                        }
                    }

                    highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
                    highlightFollowsCurrentItem: true
                }
            }
            Column {
                Text {text: "<b>Mechanisms: </b>"; font.pointSize: fontSize}

                ListView {
                    width: (page.width - methodsRow.spacing) / 2
                    height: page.height / 3
                    model: MechsAvailable
                    clip: true
                    delegate: Text { text: modelData; font.pointSize: fontSize; height: 30 }
                }
            }
        }
        Row {
            id: identitiesRow
            width: page.width
            spacing:  10
            Column {
                Text {text: "<b>Existing identities: </b>"; font.pointSize: fontSize}

                ListView {
                    id: identitiesView
                    width: (page.width - identitiesRow.spacing) / 2
                    height: (page.height / 3) - 10
                    model: IdentitiesAvailable
                    clip: true
                    delegate: Text { text: caption; font.pointSize: fontSize; width: parent.width
                        MouseArea { anchors.fill: parent
                            onClicked: {
                                identitiesView.currentIndex = index
                                SignonExample.showIdentityMethods(index)
                            }
                        }
                    }

                    highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
                    highlightFollowsCurrentItem: true
                }
            }
            Column {
                Text {text: "<b>Identity methods: </b>"; font.pointSize: fontSize}

                ListView {
                    id: identityMethodsView
                    width: (page.width - identitiesRow.spacing) / 2
                    height: (page.height / 3) - 10
                    model: IdentityMethods
                    clip: true
                    delegate: Text { text: modelData; font.pointSize: fontSize }
                }
            }
        }
        Row {
            Button {
                    text: "Create Identity"
                    width: (page.width - 20) / 3
                    onClicked: { openFile("CreateIdentity.qml") }
            }
            Button {
                    text: "Challenge"
                    width: (page.width - 20) / 3
                    enabled: identitiesView.currentIndex >= 0 && identityMethodsView.currentIndex >= 0 ? ( identityMethodsView.currentItem.text == "google" ? true : false) : false
                    onClicked: { SignonExample.challengeIdentity(identitiesView.currentIndex) }
            }
            Button {
                    text: "Remove"
                    width: (page.width - 20) / 3
                    enabled: identitiesView.currentIndex < 0 ? false : true
                    onClicked: {
                        SignonExample.removeIdentity(identitiesView.currentIndex);
                        identitiesView.currentIndex = -1;
                    }
            }
        }
    }
}
